package dao;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 创建BaseDao类，
 * 实现数据库连接和关闭功能。
 */
public class BaseDao {
    private static String driver;   //数据库驱动字符串
    private static String url;      //数据库链接字符串
    private static String user;     //数据库用户名
    private static String password; //数据库密码

    protected Connection conn = null;
    protected PreparedStatement ps = null;
    protected ResultSet rs = null;

    static {
        init();
    }

    /**
     * 从配置文件中读取连接参数
     */
    public static void init() {
        Properties pro = null;
        try {
            pro = new Properties();

            String file = "database.properties";

            InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(file);

            pro.load(is);

        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            driver = pro.getProperty("driver");
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
        }


    }

    /**
     * 1、打开数据库,连接数据库
     */
    public Connection getconn() {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    /**
     * 2、关闭数据库
     */
    public void closeAll(ResultSet rs, PreparedStatement ps, Connection conn) {
        if (null != rs) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (null != ps) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (null != conn) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    /**
     * 3、增删改 通用方法
     */
    public int executeUpdate(String sql, Object[] param) {

        int num = 0; //返回影响的行数

        conn = this.getconn();

        try {
            ps = conn.prepareStatement(sql);
            //为参数赋值
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    ps.setObject(i + 1, param[i]);
                }
            }
            //执行sql语句
            num = ps.executeUpdate();

        } catch (SQLException e) {
            System.out.println("增删改有误:"+e.getMessage());
        } finally {
            //关闭连接
            this.closeAll(null, ps, conn);
        }
        return num;
    }

    /**
     * 查询 方法
     */
    public ResultSet executeQuery(String sql, Object[] param) {
        //连接数据库
        conn = this.getconn();

        try {
            ps = conn.prepareStatement(sql);
            //为参数赋值
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    ps.setObject(i + 1, param[i]);
                }
            }
            //执行sql语句
            rs = ps.executeQuery();

        } catch (SQLException e) {
            System.out.println("查询有误:"+e.getMessage());

        }
        return rs;
    }
}

